package com.example.trafficexam2.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.trafficexam2.R;
import com.example.trafficexam2.bean.Account;
import com.example.trafficexam2.dao.AccountDao;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

/**
 * Created by Administrator on 2018/11/22/022.
 */

public class MyAccountFragment extends Fragment {
    View view;
    private Spinner carid;
    private TextView accountmoney;
    private Button queryBalance;
    private Button recharge;
    private EditText rechargemoney;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.myaccount_fragment, container, false);
        initView();
        initData();
        return  view;
    }


    private void initView(){
        carid = view.findViewById(R.id.car_id);
        accountmoney = view.findViewById(R.id.account_money);
        queryBalance = view.findViewById(R.id.query_money);
        recharge = view.findViewById(R.id.charge_money);
        rechargemoney = view.findViewById(R.id.money_et);
    }
    private void initData(){
        getBalance();
        queryBalance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getBalance();
            }
        });
        recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recharge();
            }
        });
    }
    public void recharge(){
        final String caridnum = carid.getSelectedItem().toString();
        final String rechargeMoney = rechargemoney.getText().toString();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("UserName","user1");
            jsonObject.put("CarId",caridnum);
            jsonObject.put("Money",rechargeMoney);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url = "http://47.106.153.72:8081/transportservice/action/SetCarAccountRecharge.do";
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                rechargemoney.setText("");
                Toast.makeText(getContext(),jsonObject.optString("ERRMSG"), Toast.LENGTH_SHORT).show();
                Account account = new Account(caridnum,rechargeMoney,"admin",new Date());
                new AccountDao(getContext()).insert(account);
                getBalance();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    public void getBalance(){
        String caridnum = carid.getSelectedItem().toString();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("UserName","user1");
            jsonObject.put("CarId",caridnum);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url = "http://47.106.153.72:8081/transportservice/action/GetCarAccountBalance.do";
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                String balance = jsonObject.optString("Balance");
                accountmoney.setText(balance+"元");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void insetSql(){

    }
}
